/**
 * 
 */
package neptune.web.client.controller;

import com.google.gwt.user.client.rpc.AsyncCallback;

import neptune.web.client.model.SerializableUser;
import neptune.web.client.services.user.UserService;
import neptune.web.client.view.ConsoleManager;
import neptune.web.client.view.OperativeView;
import neptune.web.client.view.SuperView;
import neptune.web.client.view.UserView;
import neptune.web.client.view.ViewManager;
import neptune.web.client.view.components.dialogs.PropertiesDialog;
import neptune.web.client.view.components.dialogs.PropertiesDialogListener;

/**
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class UserDetailsDialogController implements PropertiesDialogListener {

	/*
	 * (non-Javadoc)
	 * 
	 * @see neptune.web.client.view.components.dialogs.PropertiesDialogListener#okPressed(java.lang.String[][],
	 *      neptune.web.client.view.components.dialogs.PropertiesDialog)
	 */
	public void okPressed(final String[][] properties,
			final PropertiesDialog sender) {

		SerializableUser user = new SerializableUser();
		user.setUsername(properties[0][1]);
		user.setPassword(properties[1][1]);
		user.setRole(properties[2][1]);
		user.setEmail(properties[3][1]);
		UserService.Util.getInstance().updateUser(user, new AsyncCallback<Boolean>() {

			public void onSuccess(Boolean arg0) {
				String message = "User " + properties[0][1] + " updated";
				ConsoleManager.addConsoleMessage(message, false);
				sender.showMessage(message);

				//Aggiorna la lista degli utenti nella UserView
				SuperView operative = (SuperView) ViewManager.getViewManager().getViewByName(
						OperativeView.DEFAULT_TOKEN);
				UserView view = (UserView) operative.getViewByName(UserView.DEFAULT_TOKEN);
				view.update();
			}

			public void onFailure(Throwable arg0) {
				String message = "Impossible to update user "
						+ properties[0][1] + " Cause: " + arg0.getMessage();
				ConsoleManager.addConsoleMessage(message, true);
				sender.showMessage(message);
			}

		});
	}

}
